package liu.thread.thread.circulation;

/**
 * 1、子线程循环10次，接着主线程循环100，
 * 接着又回到子线程循环10次，接着再回到主线程又循环100，
 * 如此循环50次，请写出程序。
 * @author My
 *
 */
public class Resource {
	
	private int mc;
	private int sc;
	private int mainCount=0;
	private int mainEnd=0;
	private int subCount=0;
	private int subEnd=0;
	
	public synchronized boolean thMain() throws InterruptedException{
		
		//切换
		if(mainCount==100){
			mainCount=0;
			mainEnd++;
			notify();
			wait();
		}
		
		System.out.println(Thread.currentThread().getName()+":"+mc);
		
		mainCount++;
		mc++;
		
		//
		if(mainEnd==50){
			notify();
			return false;
		}
		return true;
	}
	
	
	public synchronized boolean thrSub() throws InterruptedException{
		
		//切换
		if(subCount==10){
			subCount=0;
			subEnd++;
			notify();
			wait();
		}
		System.out.println(Thread.currentThread().getName()+":"+sc);
		subCount++;
		sc++;
		//
		if(subEnd==50){
			notify();
			return false;
		}
		return true;
	}

}
